from jili.tool import isnumber
def str_find_num(a,key,isback=False,split_prekey=None,split_endkey=None):
    rst=None
    if split_prekey:
        s=a.split(split_prekey)
        a=s[-1]
    if split_endkey:
        s = a.split(split_endkey)
        a = s[0]
    if isback:
        n1 = a.find(key)
        if n1 > 0:
            n0 = n1 - 1
            while n0 >= 0:
                if not isnumber(a[n0]):
                    n0 = n0 + 1
                    break
                n0 = n0 - 1
            if n0 < 0:
                n0 = 0
            if n0 >= 0 and n0 < n1:
                rst = int(a[n0:n1])
        if rst   is None:
            n1=n1+len(key)
            a1 = a[n1:]
            if key in a1:
                rst = str_find_num(a1,key,isback=isback,split_prekey=split_prekey,split_endkey=split_endkey)
    else:
        n0 = a.find(key)
        n0 = n0 + len(key)
        n1 = n0
        n = len(a)
        while n1 < n:
            if not isnumber(a[n1]):
                break
            else:
                n1 = n1 + 1
        if n1 > n0:
            rst = int(a[n0:n1])
        if rst   is None:
            a1 = a[n0:]
            if key in a1:
                rst = str_find_num(a1,key,isback=isback,split_prekey=split_prekey,split_endkey=split_endkey)
    return rst
def str_find_num_byrule(a,rule):
    rst = None
    for i in rule:
        if isinstance(i,dict):
            i["a"]=a
            rst = str_find_num(**i)
        # elif isinstance(i,list):
        #     i.insert(0, a)
        #     rst = str_find_num(*i)
        else:
            ii=[a]
            for j in i:
                ii.append(j)
            rst = str_find_num(*ii)
        if rst   is not None:
            break
    return rst
if __name__=="__main__":
    rule = [
        ["条", True],
        ["单次最大", False]
    ]
    rule1 = [
               {"key":"条","isback": True},
           {"key":"单次最大", "isback": False}
    ]
    rule2 = [
        ("条", True),
        ("单次最大", False)
    ]
    print(str_find_num_byrule("100条",rule))
    print(str_find_num_byrule("单次最大100",rule))
    print(str_find_num_byrule("100条",rule1))
    print(str_find_num_byrule("单次最大100",rule1))
    print(str_find_num_byrule("100条",rule2))
    print(str_find_num_byrule("单次最大100",rule2))


    # print("100积分",str_find_num("100积分","积分",isback=True,split_prekey=None,split_endkey=None))
    # print("是100积分", str_find_num("是100积分", "积分", isback=True, split_prekey=None, split_endkey=None))
    # print("是积分100积分", str_find_num("是积分100积分", "积分", isback=True, split_prekey=None, split_endkey=None))
    # print("是积分100积分", str_find_num("是积分100积分", "积分", isback=False, split_prekey=None, split_endkey=None))
    # print("是积分100", str_find_num("是积分100", "积分", isback=False, split_prekey=None, split_endkey=None))
    # print("是积分100,最大积分500", str_find_num("是积分100,最大积分500", "积分", isback=False, split_prekey="最大", split_endkey=None))
    # print("是积分100,最大积分500", str_find_num("是积分100,最大积分500", "积分", isback=False, split_prekey=None, split_endkey="最大"))